package com.woniu.user.controller;


import cn.hutool.core.lang.Assert;
import com.woniu.user.commons.Result;
import com.woniu.user.controller.form.AddCommentForm;
import com.woniu.user.model.Comment;
import com.woniu.user.service.CommentService;
import com.woniu.user.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.*;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/comment")
@Slf4j
@Api(tags = "场馆回复评论")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PutMapping("/reply")
    @ApiOperation("回复评论")
    public Result reply(@RequestBody Map<String, Object> myMap) {
        Integer id = (Integer) myMap.get("articleId");
        String content = (String) myMap.get("repComment");
        //Integer id,String content
        log.info("进入了回复的方法 评论id{},回复内容{}", id, content);
        Assert.notNull(id, "非法请求，id为空");
        Assert.notBlank(content, "非法请求，回复内容为空");
        Boolean result = commentService.reply(id, content);
        return Result.success(200, "回复成功", result);
    }


    @GetMapping("/query")
    public Result query(Integer articleId) {
        log.info("查询{}文章的评论", articleId);
        List<Comment> comments = commentService.query(articleId);
        return Result.success(200, "查询成功", comments);
    }

    /**
     * 添加评论
     * @param addCommentForm
     * @return
     */
    @PostMapping("/addComment")
    @ApiOperation("添加评论")
    public Result addComment(@RequestBody AddCommentForm addCommentForm, HttpServletRequest req){
        Integer userId = JwtUtil.getUserIdFromReq(req);// 获取用户账号
        log.info("{}执行了添加评论操作 {}",userId,addCommentForm);
        Boolean result= commentService.addComment(userId,addCommentForm);
        return Result.success(200, "添加评论成功", result);
    }
}

